package net.sourceforge.krama.introspection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 
 * @author chabane
 * 
 */
public class TReflection {

	/**
	 * This method allows to obtains a object from TMethod object and with
	 * parameter's
	 * 
	 * @param tMethod
	 * @param paramValue
	 * @return Object
	 */
	public static Object getResult(TMethod tMethod, Object[] paramValue) {
		Object result = null;
		try {
			Class<?> className = Class.forName(tMethod.getClasse());
			Object classInstance = className.newInstance();
			List<String> paramsList = tMethod.getParams();
			Class[] parameterTypes = null;
			
			if (paramValue != null && paramValue.length > 0) {
				parameterTypes = new Class[paramsList.size()];
				Object[] arg = new Object[paramsList.size()];
				for (int i = 0; i < paramsList.size(); i++) {
					Class<?> typeParam = Class.forName(paramsList.get(i));
					parameterTypes[i] = typeParam;
				}
			}

			Method methodeName = classInstance.getClass().getDeclaredMethod(
					tMethod.getMethode(), parameterTypes);
			result = methodeName.invoke(classInstance, (Object[]) paramValue);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * Obtains
	 * 
	 * @param view
	 * @return Object
	 */
	public static Object getView(String view) {
		Object result = null;
		try {
			Class<?> className = Class.forName(view);
			result = className.newInstance();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
